/*
* main.c
*
* Copyright © 2021 vetal .
*
* This file is part of OpenGL_VAO.
*
* OpenGL_VAO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenGL_VAO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenGL_VAO. If not, see <http ://www.gnu.org/licenses/>.
*/


/*
	First sample of OpenGL and X Window system

*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <X11/X.h>
#include <X11/keysymdef.h>
#include <X11/Xlib.h>

#define GL_GLEXT_PROTOTYPES

#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include <GL/glcorearb.h>

#include "vertexes.h"
#include "shaders.h"

Display* dpy;
Window root;
GLint att[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
XVisualInfo* vi;
Colormap cmap;
XSetWindowAttributes swa;
Window win;
GLXContext glc;
XWindowAttributes gwa;
XEvent xev;



int main(int argc, char* argv[]) {

	if(argc != 3) {
		printf("syntax: %s <vertex shader file> <fragment shader file>\n", argv[0]);
		return 0;
	}
	dpy = XOpenDisplay(NULL);
	if(dpy == NULL) {
		printf("\n\tCannot connect to X server\n\n");
		exit(0);
	};

	root = DefaultRootWindow(dpy);
	vi = glXChooseVisual(dpy, 0, att);
	if(vi == NULL) {
		printf("\n\tno appropriate visual found\n\n");
		exit(0);
	} else {
		printf("\n\tvisual %p selected\n", (void*)vi->visualid);
	};

	cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
	swa.colormap = cmap;
	swa.event_mask = KeyPressMask;

	win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);
	XMapWindow(dpy, win);
	XStoreName(dpy, win, "OpenGL and Shaders");

	glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
	glXMakeCurrent(dpy, win, glc);

	glEnable(GL_DEPTH_TEST);

	unsigned int shaderProgram = CreateShaderProgram(argv[1], argv[2]);
	float verts[] = {	0.0, 0.9, 0.0,
										0.9, -0.9, 0.0,
										-0.9, -0.9, 0.0};

	unsigned int VAO = CreateVAO(verts, sizeof(verts));

	while(1) {

		if(XCheckTypedWindowEvent(dpy, win, KeyPress, &xev)) {
			KeySym sym = XLookupKeysym(&xev.xkey, 0);
      switch(sym) {
        case XK_Escape:
					glXMakeCurrent(dpy, None, NULL);
					glXDestroyContext(dpy, glc);
					XDestroyWindow(dpy, win);
					XCloseDisplay(dpy);
					exit(0);
					break;
        default:
					break;
      };
    };

		XGetWindowAttributes(dpy, win, &gwa);

		glViewport(0, 0, gwa.width, gwa.height);

		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glUseProgram(shaderProgram);
		glBindVertexArray(VAO);
		glDrawArrays(GL_TRIANGLES, 0, 3);

		glXSwapBuffers(dpy, win);
	};
};
